#include "StdAfx.h"
#include "Aidentity.h"
#include <map>
using namespace std;

//Aidentity g_identity;
Aidentity * gidentity = NULL;//&g_identity;

class AidentityStuff
{
public:
	map<wstring, void *> mnameaddr;
	bool isExist( wstring name)
	{
		return (mnameaddr.find( name) != mnameaddr.end())?true:false;
	}
	bool add( wstring name, void *addr)
	{
		if( isExist(name)) 
			return false;
		else
		{
			mnameaddr.insert( pair<wstring, void*>(name, addr));
			return true;
		}
	}
	void del( wstring name)
	{
		map<wstring, void*>::iterator it = mnameaddr.find(name);
		if( it != mnameaddr.end())
			mnameaddr.erase(it);
	}
	void del( void * addr)
	{
		map<wstring, void*>::iterator it = find( addr);
		if( it != mnameaddr.end())
			mnameaddr.erase(it);
	}
	void * get(wstring name)
	{
		map<wstring,void*>::iterator it = mnameaddr.find(name);
		if( it == mnameaddr.end())
			return NULL;
		else
			return it->second;
	}
	map<wstring, void*>::iterator find(void *addr)
	{
		map<wstring,void*>::iterator it;
		for( it=mnameaddr.begin(); it!=mnameaddr.end(); it++)
		{
			if( addr == it->second)
				return it;
		}
		return mnameaddr.end();
	}
};

Aidentity::Aidentity(void)
{
	mstuff = new AidentityStuff();
}
// ---------------------------------------
Aidentity::~Aidentity(void)
{
	if( mstuff ) { delete mstuff; mstuff = NULL; }
}
void * Aidentity::find( wstring name)
{
	return mstuff->get(name);
}
bool Aidentity::add( wstring name, void * addr)
{
	return mstuff->add(name, addr);
}
void Aidentity::del( wstring name)
{
	mstuff->del(name);
}
void Aidentity::del( void *addr)
{
	mstuff->del(addr);
}
